#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_USERS 100
#define MAX_PRODUCTS 100
#define MAX_ORDERS 100
#define RESTOCK_THRESHOLD 10
#define CLEAR_SCREEN system("cls")

// 订单状态枚举
typedef enum {
    UNPAID, PAID, SHIPPED, COMPLETED
} OrderStatus;

// 用户信息结构体
typedef struct {
    int userId;
    char name[50];
    char address[100];
    char contact[20];
} User;

// 商品信息结构体
typedef struct {
    int productId;
    char name[50];
    char category[50];
    float price;
    int stock;
    int sales;
} Product;

// 订单信息结构体
typedef struct {
    int orderId;
    int userId;
    int productId;
    int quantity;
    time_t orderTime;
    OrderStatus status;
} Order;

// 全局数据
User users[MAX_USERS];
int userCount = 0;

Product products[MAX_PRODUCTS];
int productCount = 0;

Order orders[MAX_ORDERS];
int orderCount = 0;

// 函数声明
void showDashboard();
void showUserManagement();
void showProductManagement();
void showOrderManagement();
void showInventoryManagement();
void showSalesReports();
void addUser();
void viewUsers();
void addProduct();
void viewProducts();
void createOrder();
void viewOrders();
void viewInventory();
void processRestock();
void viewSalesTrend();
void viewBestSellers();
void initData();
void showMainMenu();
int getIntInput();
char* getStatusName(OrderStatus status);
void pauseExecution();

// 获取订单状态名称
char* getStatusName(OrderStatus status) {
    switch (status) {
        case UNPAID: return "未付款";
        case PAID: return "已付款";
        case SHIPPED: return "已发货";
        case COMPLETED: return "已完成";
        default: return "未知状态";
    }
}

// 初始化示例数据
void initData() {
    // 添加用户
    strcpy(users[0].name, "张三");
    strcpy(users[0].address, "北京市朝阳区");
    strcpy(users[0].contact, "13800138000");
    users[0].userId = 1;
    userCount++;
    
    strcpy(users[1].name, "李四");
    strcpy(users[1].address, "上海市浦东新区");
    strcpy(users[1].contact, "13900139000");
    users[1].userId = 2;
    userCount++;
    
    // 添加商品
    strcpy(products[0].name, "iPhone 14");
    strcpy(products[0].category, "手机");
    products[0].price = 8999.0;
    products[0].stock = 50;
    products[0].productId = 1;
    productCount++;
    
    strcpy(products[1].name, "MacBook Pro");
    strcpy(products[1].category, "笔记本");
    products[1].price = 12999.0;
    products[1].stock = 30;
    products[1].productId = 2;
    productCount++;
    
    strcpy(products[2].name, "T恤");
    strcpy(products[2].category, "服装");
    products[2].price = 99.0;
    products[2].stock = 100;
    products[2].productId = 3;
    productCount++;
    
    // 添加订单
    orders[0].orderId = 1;
    orders[0].userId = 1;
    orders[0].productId = 1;
    orders[0].quantity = 2;
    orders[0].orderTime = time(NULL) - 3600;
    orders[0].status = COMPLETED;
    orderCount++;
    
    orders[1].orderId = 2;
    orders[1].userId = 2;
    orders[1].productId = 2;
    orders[1].quantity = 1;
    orders[1].orderTime = time(NULL) - 1800;
    orders[1].status = PAID;
    orderCount++;
    
    // 更新商品销量
    products[0].sales = 2;
    products[1].sales = 1;
}

// 获取整数输入
int getIntInput() {
    int input;
    scanf("%d", &input);
    getchar(); // 消耗换行符
    return input;
}

// 暂停执行
void pauseExecution() {
    printf("\n按Enter键继续...");
    getchar();
}

// 显示仪表盘
void showDashboard() {
    CLEAR_SCREEN;
    printf("==================== 电商平台订单处理与库存管理系统 - 仪表盘 ====================\n\n");
    
    printf("数据概览:\n");
    printf("------------------------------------\n");
    printf("| 用户总数: %d (较上月增长 10%%)       |\n", userCount);
    printf("| 商品总数: %d (较上月增长 5%%)        |\n", productCount);
    printf("| 订单总数: %d (较上月减少 2%%)        |\n", orderCount);
    
    int lowStockCount = 0;
    for (int i = 0; i < productCount; i++) {
        if (products[i].stock < RESTOCK_THRESHOLD) {
            lowStockCount++;
        }
    }
    printf("| 低库存商品: %d (较上月增长 15%%)      |\n", lowStockCount);
    printf("------------------------------------\n\n");
    
    printf("最近订单:\n");
    printf("------------------------------------\n");
    printf("订单ID\t用户\t\t商品\t\t数量\t状态\t\t时间\n");
    printf("------------------------------------\n");
    
    int orderCountToShow = orderCount < 2 ? orderCount : 2;
    for (int i = 0; i < orderCountToShow; i++) {
        char timeStr[30];
        struct tm *timeInfo = localtime(&orders[i].orderTime);
        strftime(timeStr, sizeof(timeStr), "%H:%M:%S", timeInfo);
        
        printf("%d\t%-10s\t%-15s\t%d\t%-10s\t%s\n", 
               orders[i].orderId, 
               users[orders[i].userId - 1].name,
               products[orders[i].productId - 1].name,
               orders[i].quantity,
               getStatusName(orders[i].status),
               timeStr);
    }
    printf("------------------------------------\n\n");
    
    printf("商品销售排行:\n");
    printf("------------------------------------\n");
    printf("1. %s - %d 件\n", products[0].name, products[0].sales);
    printf("2. %s - %d 件\n", products[1].name, products[1].sales);
    printf("3. %s - %d 件\n", products[2].name, products[2].sales);
    printf("------------------------------------\n\n");
    
    printf("1. 用户管理\n");
    printf("2. 商品管理\n");
    printf("3. 订单管理\n");
    printf("4. 库存管理\n");
    printf("5. 销售统计\n");
    printf("0. 返回主菜单\n\n");
    printf("请选择: ");
    
    int choice = getIntInput();
    switch (choice) {
        case 1: showUserManagement(); break;
        case 2: showProductManagement(); break;
        case 3: showOrderManagement(); break;
        case 4: showInventoryManagement(); break;
        case 5: showSalesReports(); break;
        case 0: showMainMenu(); break;
        default: printf("无效选择！"); pauseExecution(); showDashboard();
    }
}

// 显示用户管理
void showUserManagement() {
    CLEAR_SCREEN;
    printf("==================== 电商平台订单处理与库存管理系统 - 用户管理 ====================\n\n");
    
    printf("用户列表:\n");
    printf("------------------------------------\n");
    printf("用户ID\t姓名\t\t地址\t\t\t联系方式\n");
    printf("------------------------------------\n");
    
    for (int i = 0; i < userCount; i++) {
        printf("%d\t%-10s\t%-30s\t%s\n", 
               users[i].userId, 
               users[i].name,
               users[i].address,
               users[i].contact);
    }
    printf("------------------------------------\n\n");
    
    printf("1. 添加用户\n");
    printf("0. 返回上一级\n\n");
    printf("请选择: ");
    
    int choice = getIntInput();
    switch (choice) {
        case 1: addUser(); break;
        case 0: showDashboard(); break;
        default: printf("无效选择！"); pauseExecution(); showUserManagement();
    }
}

// 添加用户
void addUser() {
    CLEAR_SCREEN;
    printf("==================== 电商平台订单处理与库存管理系统 - 添加用户 ====================\n\n");
    
    if (userCount >= MAX_USERS) {
        printf("用户数量已达上限！\n");
        pauseExecution();
        showUserManagement();
        return;
    }
    
    printf("请输入用户ID: ");
    int userId = getIntInput();
    
    // 检查用户ID是否已存在
    for (int i = 0; i < userCount; i++) {
        if (users[i].userId == userId) {
            printf("该用户ID已存在！\n");
            pauseExecution();
            showUserManagement();
            return;
        }
    }
    
    printf("请输入用户姓名: ");
    getchar(); // 消耗换行符
    fgets(users[userCount].name, 50, stdin);
    users[userCount].name[strcspn(users[userCount].name, "\n")] = 0; // 移除换行符
    
    printf("请输入用户地址: ");
    fgets(users[userCount].address, 100, stdin);
    users[userCount].address[strcspn(users[userCount].address, "\n")] = 0;
    
    printf("请输入联系方式: ");
    fgets(users[userCount].contact, 20, stdin);
    users[userCount].contact[strcspn(users[userCount].contact, "\n")] = 0;
    
    users[userCount].userId = userId;
    userCount++;
    
    printf("用户添加成功！\n");
    pauseExecution();
    showUserManagement();
}

// 显示商品管理
void showProductManagement() {
    CLEAR_SCREEN;
    printf("==================== 电商平台订单处理与库存管理系统 - 商品管理 ====================\n\n");
    
    printf("商品列表:\n");
    printf("------------------------------------\n");
    printf("商品ID\t名称\t\t类别\t\t价格\t库存\t销量\n");
    printf("------------------------------------\n");
    
    for (int i = 0; i < productCount; i++) {
        printf("%d\t%-15s\t%-10s\t%.2f\t%d\t%d\n", 
               products[i].productId, 
               products[i].name,
               products[i].category,
               products[i].price,
               products[i].stock,
               products[i].sales);
    }
    printf("------------------------------------\n\n");
    
    printf("1. 添加商品\n");
    printf("0. 返回上一级\n\n");
    printf("请选择: ");
    
    int choice = getIntInput();
    switch (choice) {
        case 1: addProduct(); break;
        case 0: showDashboard(); break;
        default: printf("无效选择！"); pauseExecution(); showProductManagement();
    }
}

// 添加商品
void addProduct() {
    CLEAR_SCREEN;
    printf("==================== 电商平台订单处理与库存管理系统 - 添加商品 ====================\n\n");